<template>
  <div>
    <!-- 索赔结算汇总审核 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      columnsRef="ClaimAccountSummaryChecklgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary"  :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="editFlag" :title="'索赔结算汇总提报审核'" destroyOnClose width="60%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <EditForm @close="Editclose" @save="Editsave" :rowData="rowData"></EditForm>
    </Drawer>
    <Drawer :visible.sync="detailFlag" :title="'索赔结算汇总提报详情'" destroyOnClose width="60%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DetailForm @close="detailclose" @save="detailsave" :rowData="rowData"></DetailForm>
    </Drawer>
  </div>
</template>
<script>
  import EditForm from './EditForm.vue';
  import DetailForm from './DetailForm.vue';
  import { exportExcelFile } from '@/utils/index';
  import { getListByPage, getPartType,getListForExport ,summaryExportOfStatement,exporturl} from '@/api/claim/factory/ClaimAccountSummaryCheck';
  import {  getAppId } from '@/assets/js/auth';
  import { dictionary } from '@/utils/dictMixin';
  import moment from "moment";
  export default {
    mixins: [dictionary],
    components: {
     EditForm,DetailForm
    },
    data() {
      return {
        dataList: [],
        rowData: {},
        visible: false,
        detailFlag: false,
        editFlag: false,
        collapse: false,
        infoId: null,
        fetchapi: getListByPage,
        topFilterInit: this.topFilterInitFun(),
        flterValue: {}, //参数
        columns: this.creatHeader(),
        fetchParams: { sumStatus:55471002, xhrAbort: true }, //ipa 接口
        selectedRows: [], //选中经销商
        dialogTitle: '选择车辆',
        visibleAdd: false
      };
    },
    created() {
      if (getAppId() == 'arcfox') {
        this.topFilterInit.find(x => x.fieldName == 'sumType').itemList =this.createDictList('5543',[55431001])
      }else{
        this.topFilterInit.find(x => x.fieldName == 'sumType').itemList =this.createDictList('5543')
      }
      this.fetchParams.beginApplyDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
      this.fetchParams.endApplyDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
      // this.chuqi();
      this.chuqi2();
    },
    methods: {
      //翻译
      // async chuqi() {
      //   const res = await getPartType({ type: 5547 });
      //   if (res.resultCode == 200) {
      //     this.topFilterInit.find(x => x.fieldName == 'sumStatus').initialValue = '';
      //     this.topFilterInit.find(x => x.fieldName == 'sumStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      //   }
      // },

      //考核分类
      async chuqi2() {
        // const res = await getPartType({ type: 5543 });
        // if(getAppId()=='arcfox') {
        //   delete res.data[0]
        // }
        // if (res.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'sumType').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'sumType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        // }
      },
      //选中
      selectChange(rows) {
        this.selectedRows = rows;
        console.log(this.selectedRows);
      },
      close() {
        this.visible = false;
      },
      chooseOrderNo(val) {
        this.editFlag = true;
        this.rowData = val;
      },
      showDetailFun(val)  {
        this.detailFlag = true;
        this.rowData = val;
      },

      ExportRow(val)  {
        console.log("导出VAL",val)
        console.log("汇总单号",val.sumNo);
        // let d=summaryExportOfStatement({"sumNo":val.sumNo});
        // console.log("导出信息",d);
        summaryExportOfStatement({"sumNo":val.sumNo}).then(res => {
          exportExcelFile(res.data, '索赔结算汇总单信息');
        });


      },
      save(obj) {
        this.topFilterInit.find(x => x.fieldName == 'model').initialValue = obj[0].address;
        this.visible = false;
      },
      // 导出
     async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000147' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
        // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[0];
        // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[1];
        // if(!beginSubmitDate) {
        //   this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
        //   return
        // }
        // if(!endSubmitDate) {
        //   this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
        //   return
        // }
        // if(getAppId() == 'arcfox') {
        //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
        //     return
        //   }
        // } else {
        //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
        //     return
        //   }
        // }
        // let obj = Object.assign({}, this.fetchParams, { sumStatus:55471002, xhrAbort: false });
        // obj.beginApplyDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[0];
        // obj.endApplyDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[1];
        // getListForExport(obj).then(res => {
        //   exportExcelFile(res.data, '索赔结算汇总单信息');
        // });
      },
      Editclose() {
        this.editFlag = false;
      },
      Editsave() {
        this.editFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },
      //查看
      detailsave() {
        this.detailFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },
      detailclose() {
        this.detailFlag = false;
      },
      changeHandle(val) {
        if(!val.beginApplyDate) {
          this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
          return
        }
        if(!val.endApplyDate) {
          this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
          return
        }
        if(getAppId() == 'arcfox') {
          if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
            return
          }
        } else {
          if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
            return
          }
        }
        this.fetchParams = Object.assign({}, this.fetchParams, val,{ sumStatus:55471002, xhrAbort: false });
      },
      //搜索数据
      topFilterInitFun() {
        return [
          { type: 'INPUT', label: '结算汇总单号', fieldName: 'sumNo', placeholder: '请输入结算汇总单号...', initialValue: '' },
          {
            type: 'RANGE_DATE',
            label: '提交时间',
            fieldName: 'beginApplyDate|endApplyDate',
            placeholder: '请输入',
            initialValue: [
              moment()
                .startOf('week')
                .format('YYYY-MM-DD HH:mm:ss'),
              moment()
                .endOf('week')
                .format('YYYY-MM-DD HH:mm:ss')
            ],
            rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
          },
          { type: 'SELECT', label: '索赔车辆属性', fieldName: 'sumType', placeholder: '请选择索赔车辆属性...', initialValue: '', itemList: [] }
        ];
      },
      //table表头和数据源
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            fixed: 'left',
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            width: 120,
            dataIndex: 'column-action',
            render: (props, h) => {
              return (
                <div>
                  <multiuse-button size="mini" type="text"
                                   disabled={((props.row.sumStatus == '55471002')) ? false : true}
                                   onClick={() => this.chooseOrderNo(props.row)}>
                    审核
                  </multiuse-button>
                  <multiuse-button size="mini" type="text" onClick={() => this.showDetailFun(props.row)}>
                    查看
                  </multiuse-button>
                  <multiuse-button size="mini" type="text" onClick={() => this.ExportRow(props.row)}>
                    导出
                  </multiuse-button>
                </div>
            );
            }
          },
          { title: '索赔车辆属性', dataIndex: 'sumTypeStr', sorter: true },
          { title: '结算汇总单号', dataIndex: 'sumNo', sorter: true },
          { title: '单据状态', dataIndex: 'statusStr', sorter: true },
          { title: '结算单数', dataIndex: 'settleNum', sorter: true },
          { title: '发票张数', dataIndex: 'invoiceSheets', sorter: true },
          { title: '结算总金额', dataIndex: 'invoiceAmount', sorter: true },
          { title: '索赔金额', dataIndex: 'claimSum', sorter: true },
          { title: '考核金额', dataIndex: 'assessSum', sorter: true },
          // { title: '正常索赔金额', dataIndex: 'zhengChangAmount', sorter: true },
          // { title: '免费保养金额', dataIndex: 'mianFeiAmount', sorter: true },
          // { title: '新车PDI金额', dataIndex: 'xinCheAmount', sorter: true },
          // { title: '技术支持金额', dataIndex: 'jiShuAmount', sorter: true },
          // { title: '服务活动金额', dataIndex: 'fuWuAmount', sorter: true },
          // { title: '让渡服务金额', dataIndex: 'rangDuAmount', sorter: true },
          { title: '提交时间', dataIndex: 'applyDate', sorter: true },
          { title: '审核时间', dataIndex: 'approveDate', sorter: true },
          // { title: '主键', dataIndex: 'sumId', hidden: true },
          // { title: '状态', dataIndex: 'sumStatus', hidden: true }
        ];
      }
    }
  };
</script>
<style lang="less" scoped></style>
